package com.wechat.back.controller;

import com.wechat.back.anno.OptLog;
import com.wechat.back.pojo.pic.Icons;
import com.wechat.back.pojo.tools.Result;
import com.wechat.back.service.IconsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/icons")
public class IconsController {
    @Autowired
    private IconsService iconService;

    @GetMapping("/list")
    public Result<List<Icons>> List(
            @RequestParam(required = false) Integer iconId,
            @RequestParam(required = false) String iconType
    ){
        List<Icons> icons = iconService.list(iconId,iconType);
        return Result.success(icons);
    }

    @OptLog(optModule = "图标模块",optType = "添加", optDesc = "添加图标")
    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Icons.Add.class) Icons icons){
        //如果Icons不为空，则添加
        try {
            iconService.add(icons);
            return Result.success();
        } catch (Exception  e) {
            return Result.error("校验失败，请检查输入数据");
        }
    }

    @OptLog(optModule = "图标模块",optType = "修改", optDesc = "修改图标")
    @PutMapping
    public Result update(@RequestBody @Validated(Icons.Update.class) Icons icons){
        iconService.update(icons);
        return Result.success();
    }

    @OptLog(optModule = "图标模块",optType = "删除", optDesc = "删除图标")
    @DeleteMapping
    public Result delete(Integer iconId){
        iconService.delete(iconId);
        return Result.success();
    }
}